package com.jy.adscm.operation.purchase.controller;


import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.purchase.bean.*;
import com.jy.adscm.operation.purchase.service.MachinetypeService;
import com.jy.adscm.operation.purchase.service.PurchasePlanService;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.zltx.bean.JyDrawingEditInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 机器型号bom对应数量的配置表 前端控制器
 * </p>
 *
 * @author lzf
 * @since 2019-05-05
 */
@RestController
@RequestMapping("adscm/operation/machinetype")
public class MachinetypeController {
    @Autowired
    MachinetypeService machinetypeService;
@Autowired
    PurchasePlanService purchasePlanService;
    @RequestMapping("insert")

    public SimpleCoreResponse insert(@RequestBody  Machinetype machinetype ){
//        List<Machinetype> list1 = JSON.parseArray(list, Machinetype.class);
//        boolean result=false;
//        for (Machinetype machinetype : list) {
//            result = machinetypeService.insert(machinetype);
//        }
//        machinetype.setTotalTime();
        return new SimpleCoreResponse("200","新增成功",machinetypeService.insert(machinetype));
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody Machinetype machinetype){
        return new SimpleCoreResponse("200","修改成功",machinetypeService.updateById(machinetype));
    }
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable("id")Long id){
        return new SimpleCoreResponse("200","删除成功",machinetypeService.deleteById(id));
    }

    @RequestMapping("queryByBom")
    public SimpleCoreResponse query(@RequestParam(value = "key",required = false)String key){
        return new SimpleCoreResponse("200","查询bom清单成功",machinetypeService.queryByBom(key));
    }

    @RequestMapping("queryByMachineType/{type}")
    public SimpleCoreResponse queryByMachineType(@PathVariable String type) throws AdscmSystemException {
        return new SimpleCoreResponse("200","查询码表成功",machinetypeService.queryByMachineType(type));
    }

    @RequestMapping("queryMachine")
    public SimpleCoreResponse queryMachine(){
        return new SimpleCoreResponse("200","查询机器列表成功",machinetypeService.queryMachineList());
    }

    @RequestMapping ("queryByPage")
    public SimpleCoreResponse queryConfig(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                          @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "sortBy", required = false) String sortBy,
                                          @RequestParam(value = "desc",required = false)boolean desc,
                                          @RequestParam(value = "key", required = false) String key,
                                          @RequestParam(value = "code",required = true) String code){
        QueryByPageParameter queryByPageParameter=new QueryByPageParameter(currentPage,pageSize,sortBy,desc,key);
        return new SimpleCoreResponse("200","查询配置表成功",machinetypeService.queryConfig(queryByPageParameter,code));
    }

    @RequestMapping("queryByParentId")
    public SimpleCoreResponse queryByParentId(@RequestParam(value = "pid",defaultValue = "0")Long pid,
                                              @RequestParam(value="code", required = true) String code){
        return new SimpleCoreResponse("200","查询物料一级目录成功",machinetypeService.queryByParentId(pid,code));
    }

    @RequestMapping("createPlan")
    public SimpleCoreResponse createPlan(@RequestBody CreatePlan createPlan) throws Exception{
        return new SimpleCoreResponse("200","采购计划生成成功",machinetypeService.createPlan(createPlan));
    }

    @RequestMapping("queryByPlan")
    public SimpleCoreResponse queryByPlan(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                          @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "sortBy", required = false) String sortBy,
                                          @RequestParam(value = "desc",required = false)boolean desc,
                                          @RequestParam(value = "key", required = false) String key,
                                          @RequestParam(value = "status",defaultValue = "0") String status){
        QueryByPageParameter queryByPageParameter=new QueryByPageParameter(currentPage,pageSize,sortBy,desc,key);
        return new SimpleCoreResponse("200","查询采购计划",machinetypeService.queryByPlan(status,queryByPageParameter));
    }
    @RequestMapping("updateByPlan")
    public SimpleCoreResponse updateByPlan(@RequestBody List<PurchasePlan> list) throws Exception{
//
//        for (PurchasePlan plan : list) {
//            purchasePlanService.updateById(plan);
//        }

        return new SimpleCoreResponse("200","修改采购计划成功",machinetypeService.updateByPlan(list));
    }
}

